package cn.zhuizikejithreeig.com.xigeapplication;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.icu.util.Calendar;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressbackView mProgressbackView;
    private ProgressView mProgressView;
    private ImageView mProgressbackOne;
    private ImageView mProgressbackTwo;
    private TextView mTimeText;
    private LinearLayout mMiddleLine;
    private TextView mMiddleText;
    private Button mBiginButton;
    private Button mStopButton;
    private Button mContiun;
    private Button mFoger;
    private LinearLayout mButtonLine;
    private WheelView mWheelView;
    private int mins = 7 * 60 * 1000;
    private Handler handler = new Handler(Looper.getMainLooper());
    private List<String> list = new ArrayList<>();
    private Animation animationOne;
    private Animation animationTwo;
    private boolean isStart = false;
    private RelativeLayout mHandrelay;
    private MediaPlayer player;
    private int statemiss = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressbackView = (ProgressbackView) findViewById(R.id.progressbackview);
        mProgressbackOne = (ImageView) findViewById(R.id.progressbackone);
        mProgressbackTwo = (ImageView) findViewById(R.id.progressbacktwo);

        //倒计时进度
        mProgressView = (ProgressView) findViewById(R.id.progressview);
        mHandrelay = (RelativeLayout) findViewById(R.id.handrelay);

        mTimeText = (TextView) findViewById(R.id.text);
        mMiddleLine = (LinearLayout) findViewById(R.id.start);
        mMiddleText = (TextView) findViewById(R.id.middletext);

        mBiginButton = (Button) findViewById(R.id.first);
        mStopButton = (Button) findViewById(R.id.second);
        mContiun = (Button) findViewById(R.id.contiun);
        mFoger = (Button) findViewById(R.id.foger);

        mButtonLine = (LinearLayout) findViewById(R.id.thiredline);


        mBiginButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mContiun.setOnClickListener(this);
        mFoger.setOnClickListener(this);
        mHandrelay.setOnClickListener(this);

        //初始化设置
        mBiginButton.setVisibility(View.VISIBLE);
//        mMiddleLine.setVisibility(View.VISIBLE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            Calendar calendar = Calendar.getInstance();
            int apm = calendar.get(Calendar.AM_PM);
            if (apm == 0) {
                mMiddleText.setText("上午好");
            } else {
                mMiddleText.setText("下午好");
            }
        }
        mProgressbackView.setVisibility(View.VISIBLE);
        mProgressbackView.setPaintColor("#e4fff2");

        mMiddleLine.setVisibility(View.VISIBLE);
        //设置波轮滚动
        mWheelView = (WheelView) findViewById(R.id.wheelview);
        mWheelView.setVisibility(View.INVISIBLE);
        list.clear();
        list.add("01分钟");
        list.add("02分钟");
        list.add("03分钟");
        list.add("04分钟");
        list.add("05分钟");
        list.add("06分钟");
        list.add("07分钟");
        mWheelView.setCyclic(false);
        mWheelView.setAdapter(new ArrayWheelAdapter(list));
        mWheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
//                Toast.makeText(MainActivity.this, "" + list.get(index), Toast.LENGTH_SHORT).show();
                if (TextUtils.equals(list.get(index), "01分钟")) {
                    mins = 1 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "02分钟")) {
                    mins = 2 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "03分钟")) {
                    mins = 3 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "04分钟")) {
                    mins = 4 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "05分钟")) {
                    mins = 5 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "06分钟")) {
                    mins = 6 * 60 * 1000;
                } else if (TextUtils.equals(list.get(index), "07分钟")) {
                    mins = 7 * 60 * 1000;
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击开始
            case R.id.first:
                mBiginButton.setVisibility(View.GONE);
                mButtonLine.setVisibility(View.GONE);
                mStopButton.setVisibility(View.VISIBLE);
                annimationStart();
                break;
            //点击暂停
            case R.id.second:
                mBiginButton.setVisibility(View.GONE);
                mButtonLine.setVisibility(View.VISIBLE);
                mStopButton.setVisibility(View.GONE);
                CompontApplayCation.appKey = true;
                mProgressbackOne.setVisibility(View.INVISIBLE);
                mProgressbackTwo.setVisibility(View.INVISIBLE);
                animationOne = null;
                if (player!=null){
                    player.pause();
                }
                break;
            //点击继续
            case R.id.contiun:
                mBiginButton.setVisibility(View.GONE);
                mButtonLine.setVisibility(View.GONE);
                mStopButton.setVisibility(View.VISIBLE);
                mProgressbackOne.setVisibility(View.VISIBLE);
                mProgressbackTwo.setVisibility(View.VISIBLE);
                //继续动画
                animation();
                //时间也继续
                textTime(statemiss);
                if (player!=null){
                  player.start();
                }else {
                    palyMusic();
                }
                initContuin();
                break;
            //点击放弃
            case R.id.foger:
                mBiginButton.setVisibility(View.VISIBLE);
                mButtonLine.setVisibility(View.GONE);
                mStopButton.setVisibility(View.GONE);
                break;
            //当点击头像时
            case R.id.handrelay:
                mWheelView.setVisibility(View.VISIBLE);
                mMiddleLine.setVisibility(View.INVISIBLE);
                mProgressbackOne.setVisibility(View.INVISIBLE);
                mProgressbackTwo.setVisibility(View.INVISIBLE);
                mTimeText.setVisibility(View.INVISIBLE);
                mProgressView.setVisibility(View.INVISIBLE);
                isStart = true;
                showItems();
                break;
        }


    }

    //继续播放
    private void initContuin() {

        mProgressView.continueDoTime(new OnFinishListener() {
            @Override
            public void onFinish() {
                mins = 7 * 60 * 1000;
            }
        });

    }

    //但点击开始的时候,开始动画
    private void annimationStart() {
        CompontApplayCation.appKey = false;
        mMiddleLine.setVisibility(View.INVISIBLE);
        mWheelView.setVisibility(View.INVISIBLE);
        mProgressbackOne.setVisibility(View.VISIBLE);
        mProgressbackTwo.setVisibility(View.VISIBLE);
        mTimeText.setVisibility(View.VISIBLE);
        mProgressView.setVisibility(View.VISIBLE);

        mTimeText.setText(timeChenge(mins));
        mProgressView.setPaintColor("#FF4081");

        mProgressView.startDownTime(mins, new OnFinishListener() {
            @Override
            public void onFinish() {
                mins = 7 * 60 * 1000;
            }
        });
        //(1)开始倒计时
        textTime(mins);
        //(2)开始动画
        animation();
        //(3)播放音乐
        palyMusic();


    }

    private int muis;
    private void textTime( int minstime) {
        muis = minstime;

        handler.post(new Runnable() {
            @Override
            public void run() {
                muis -= 1000;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (muis<= 0) {
                            mTimeText.setText("倒计时结束");
                            return;
                        }
                        statemiss = muis;
                        mTimeText.setText(timeChenge(muis));


                    }
                });
                if (!CompontApplayCation.appKey) {
                    textTime(statemiss);
                }else {
                    handler.removeCallbacksAndMessages(null);
                }

            }
        });

    }

    private void palyMusic() {
        try {
            AssetManager assetManager = this.getAssets();
            AssetFileDescriptor afd = assetManager.openFd("completenotification.m4a");
            player = new MediaPlayer();
            player.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            player.setLooping(true);//循环播放
            player.prepare();
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void animation() {
        if (animationOne==null) {
            animationOne = AnimationUtils.loadAnimation(MainActivity.this, R.anim.player_double_click_animation);
        }
        if (animationOne!=null)
        mProgressbackOne.startAnimation(animationOne);
        if (animationOne!=null)
        animationOne.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        handler.removeCallbacksAndMessages(null);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                animationTwo = AnimationUtils.loadAnimation(MainActivity.this, R.anim.player_double_click_animation);
                                mProgressbackTwo.startAnimation(animationTwo);
                            }
                        });

                    }
                }, 2000);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (animationOne!=null) {
                    mProgressbackOne.startAnimation(animationOne);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    //设置头像点击事件
    private void showItems() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isStart) {
                    mMiddleLine.setVisibility(View.INVISIBLE);
                    mWheelView.setVisibility(View.VISIBLE);
                } else {
                    mWheelView.setVisibility(View.INVISIBLE);
                    mMiddleLine.setVisibility(View.VISIBLE);
                }
            }
        }, 2000);
    }
    // 位数不足补零
    private String fill_zero_prefix(int num) {
        return num < 10 ? "0" + num : num + "";
    }

    private String timeChenge(int time) {
        // 秒数
        int second = (time / 1000);
        //小时数
        int hr = second / 3600;
        //分钟位
        String min = fill_zero_prefix((int) (second - hr * 3600) / 60);
        // 秒位
        String sec = fill_zero_prefix((second - hr * 3600 - Integer.parseInt(min) * 60));
        return min + ":" + sec;
    }

}
